package com.hnust.root.common.base.response;

import cn.hutool.core.lang.Console;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hnust.root.common.constant.ResultConstant;
import com.hnust.root.common.model.ManagerInfo;
import com.hnust.root.common.util.JWTUtil;
import com.hnust.root.service.auth.ManagerInfoService;
import com.hnust.root.web.shiro.ShiroKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BaseRestController {
    @Autowired
    private ManagerInfoService managerInfoService;

    private static Log LOGGER = LogFactory.get();


    public  <T> BaseResult<T> fillSuccessData(T data) {
        Console.log("请求成功，服务器返回信息为：" + data);
        return new BaseResult<T>(HttpStatus.OK.value(), ResultConstant.SUCCESS_CODE,HttpStatus.OK.getReasonPhrase(), data);

    }

    public <T> BaseResult<T> fillSuccessData() {

        return new BaseResult<T>(HttpStatus.OK.value(), ResultConstant.SUCCESS_CODE, HttpStatus.OK.getReasonPhrase(), null);
    }

//    public <T> BaseResult<T> fillFailData(T data) {
//        return new BaseResult<T>(HttpStatus.BAD_REQUEST.value(),ResultConstant.ERROR_CODE, HttpStatus.BAD_REQUEST.getReasonPhrase(), data);
//    }

    /**
     * 毕设临时需求
     * @param data
     * @return
     */
    public  BaseResult fillFailData(String data) {
        return new BaseResult(HttpStatus.BAD_REQUEST.value(),ResultConstant.ERROR_CODE, HttpStatus.BAD_REQUEST.getReasonPhrase(), data);
    }

    public <T> BaseResult<T> fillFailData() {
        return new BaseResult<T>(HttpStatus.BAD_REQUEST.value(), ResultConstant.ERROR_CODE, HttpStatus.BAD_REQUEST.getReasonPhrase(), null);
    }

    /**
     * 从token中获取用户信息
     * @return
     */
    public ManagerInfo getUserInfo() {
        String token = ShiroKit.principal();
        String userName = JWTUtil.getUsername(token);
        return managerInfoService.findByUsername(userName);
    }


    /**
     * 获取用户名
     * @return
     */
    public String getUserName() {
        return getUserInfo().getUsername();
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public int getUserId() {
        return getUserInfo().getId();
    }


    //通过微信token来获取用户openID
    public String getWxAccountOpenId() {
        return ShiroKit.principal();
    }

}
